require(['./config'],()=>{
    require(['template','header','footer','jquery','jqfly'],(template,header)=>{
        class Cakelist{
            constructor(){
                this.getcakelist()
            }
            getcakelist(){
                $.get('http://www.xiongmaoyouxuan.com/api/search?word=%E7%94%9F%E6%97%A5%E8%9B%8B%E7%B3%95&start=0&sort=0&couponOnly=NaN&minPrice=0&maxPrice=99999',resp=>{
                    if(resp.code === 200){
                        const{ list } = resp.data
                        $('#cake-list-l').html(
                            template('cake-list-lTemplate',{
                                list
                            })
                        )
                        const that = this
                        $('.sure').on('click',function(e){
                            that.fly(e)
                            console.log(list)
                            let cl= $(this).parents('tr').attr('data-id')
                            cl = JSON.parse(cl)
                            let cart1 = {
                                id:cl.id,
                                check:true,
                                count:1,
                                price:cl.price,
                                photo:[{url:cl.image},{}],
                                title:cl.title
                            }
                            let cart = localStorage.getItem('cart')
                            if(cart){
                                cart = JSON.parse(cart)
                                const isExist = cart.some(shop=> shop.id === cart1.id)
                                if(isExist){
                                    cart = cart.map(shop =>{
                                        if(shop.id === cart1.id){
                                            shop.count++
                                        }
                                        return shop
                                    })
                                }else{
                                   cart.push(cart1) 
                                }
                                localStorage.setItem('cart',JSON.stringify(cart))
                            }else{
                                localStorage.setItem('cart',JSON.stringify([
                                    cart1
                                ]))
                            }
                            
                            console.log(cl)
                        })
                    }
                })
            }
            fly(e){
                $('<img class="fly" src="/img/money.gif">').fly({
                    start:{
                      left: e.clientX,  //开始位置（必填）#fly元素会被设置成position: fixed
                      top: e.clientY,  //开始位置（必填）
                    },
                    end:{
                      left: $('.cake-list-car').offset().left, //结束位置（必填）
                      top: $('.cake-list-car').offset().top,  //结束位置（必填）
                    //   width: 100, //结束时高度
                    //   height: 100, //结束时高度
                    },
                    // autoPlay: false, //是否直接运动,默认true
                    speed: 1.1, //越大越快，默认1.2
                    vertex_Rtop: 50, //运动轨迹最高点top值，默认20
                    onEnd: function(){
                         this.destroy()
                         header.calcCartCount()
                    } //结束回调
                  });
            }
            
            
        }
        new Cakelist()
    })
})